﻿Imports System.Globalization
Imports Ventrian.NewsArticles

Namespace Aricie.DNN.Services.Url.FriendlyUrl.Providers
    Public Class VentrianNewsArticlesUrlRewriter
        Implements IUrlRewriterProvider
        Implements IIsActive


        Public Function GetModuleRewrites() As List(Of GroupParamsRewrite) Implements IUrlRewriterProvider.GetRewrites
            Dim toReturn As New List(Of GroupParamsRewrite)
            Dim desc As String = "Title of the current Article, filtered according to the general parameters"
            toReturn.Add(New GroupParamsRewrite("ArticleTitle", desc, RewriteType.SubPath, UrlParam.FromSyntax("articleid")))
            desc = "Name of the current Ventrian NewsArticle Article's category, filtered according to the general parameters"
            toReturn.Add(New GroupParamsRewrite("CategoryName", desc, RewriteType.SubPath, UrlParam.FromSyntax("categoryid")))
            desc = "Title of the current Ventrian NewsArticle Article's Page being browsed, filtered according to the general parameters"
            toReturn.Add(New GroupParamsRewrite("PageTitle", desc, RewriteType.SubPath, UrlParam.FromSyntax("pageid")))
            Return toReturn
        End Function

        Public Function RewriteParams(ByVal groupName As String, ByVal objRewriteType As RewriteType, _
                                       ByVal params As Dictionary(Of UrlParam, String), ByVal behaviour As FriendlierUrlStrategy) As GroupRewriteResult _
            Implements IUrlRewriterProvider.RewriteParams
            Dim toReturn As New GroupRewriteResult()

            If params.Count > 0 Then
                Select Case groupName.ToLowerInvariant
                    Case "articletitle"
                        Dim _
                            articleId As Integer = _
                                Integer.Parse(params(UrlParam.FromSyntax("articleid")), CultureInfo.InvariantCulture)
                        toReturn.RewriteValue = GetArticleTitle(articleId, behaviour)
                        toReturn.ConsumedParameters(UrlParam.FromSyntax("articleid")) = True
                    Case "categoryname"
                        Dim _
                            categoryid As Integer = _
                                Integer.Parse(params(UrlParam.FromSyntax("categoryid")), CultureInfo.InvariantCulture)
                        toReturn.RewriteValue = GetCategoryName(categoryid, behaviour)
                        toReturn.ConsumedParameters(UrlParam.FromSyntax("categoryid")) = True
                    Case "pagetitle"
                        Dim _
                            pageId As Integer = _
                                Integer.Parse(params(UrlParam.FromSyntax("pageid")), CultureInfo.InvariantCulture)
                        toReturn.RewriteValue = GetPageTitle(pageId, behaviour)
                        toReturn.ConsumedParameters(UrlParam.FromSyntax("pageid")) = True
                End Select
            End If
            Return toReturn
        End Function


        Public Function GetArticleTitle(ByVal articleId As Integer, ByVal behaviour As FriendlierUrlStrategy) As String
            Dim controller As New ArticleController
            Dim objArt As ArticleInfo = controller.GetArticle(articleId)
            Dim toReturn As String = ""
            If objArt IsNot Nothing Then
                toReturn = objArt.Title
            End If
            'Return behaviour.StringEscaper.EscapeString(toReturn)
            Return toReturn
        End Function

        Public Function GetCategoryName(ByVal categoryId As Integer, ByVal behaviour As FriendlierUrlStrategy) As String
            Dim controller As New CategoryController
            Dim objCat As CategoryInfo = controller.GetCategory(categoryId)
            Dim toReturn As String = ""
            If objCat IsNot Nothing Then
                toReturn = objCat.Name
            End If
            'Return behaviour.StringEscaper.EscapeString(toReturn)
            Return toReturn
        End Function

        Public Function GetPageTitle(ByVal pageId As Integer, ByVal behaviour As FriendlierUrlStrategy) As String
            Dim controller As New PageController
            Dim objPage As PageInfo = controller.GetPage(pageId)
            Dim toReturn As String = ""
            If objPage IsNot Nothing Then
                toReturn = objPage.Title
            End If
            'Return behaviour.StringEscaper.EscapeString(toReturn)
            Return toReturn
        End Function

        Public ReadOnly Property IsActive As Boolean Implements IIsActive.IsActive
            Get
                Return DotNetNuke.Framework.Reflection.CreateType("Ventrian.NewsArticles.CategoryController, Ventrian.NewsArticles", True) IsNot Nothing
            End Get
        End Property
    End Class
End Namespace


